package com.mxfcode.back.controller;

import com.mxfcode.annotation.LoginRequired;
import com.mxfcode.back.service.BackPlateService;
import com.mxfcode.bbs_common.response.PageDataResult;
import com.mxfcode.bbs_common.util.AjaxJson;
import com.mxfcode.bbs_common.util.CommUtils;
import com.mxfcode.bbs_common.util.StringUtils;
import com.mxfcode.entity.BBSPlate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @类名 PlateController
 * @描述 TODO
 * @作者 mxf
 * @创建日期 2021-03-05 8:59
 **/
@Controller
@RequestMapping("/back/plate")
@LoginRequired
public class BackPlateController {

    private static final Logger logger = LoggerFactory.getLogger(BackPlateController.class);

    @Resource
    private BackPlateService backPlateService;

    @RequestMapping(value = "/plateList", method = RequestMethod.POST)
    @ResponseBody
    public PageDataResult getPlateList(@RequestParam("pageNum") Integer pageNum,
                                      @RequestParam("pageSize") Integer pageSize) {
        /*logger.info("分页查询用户列表！搜索条件：userSearch：" + userSearch + ",pageNum:" + page.getPageNum()
                + ",每页记录数量pageSize:" + page.getPageSize());*/
        PageDataResult pdr = new PageDataResult();
        try {
            if(null == pageNum) {
                pageNum = 1;
            }
            if(null == pageSize) {
                pageSize = 10;
            }
            // 获取用户列表
            pdr = backPlateService.getPlateList(pageNum ,pageSize);
            logger.info("论坛板块列表查询=pdr:" + pdr);

        } catch (Exception e) {
            logger.error("论坛板块列表查询异常！", e);
        }
        return pdr;
    }

    @RequestMapping(value = "/allPlateList", method = RequestMethod.POST)
    @ResponseBody
    public List<BBSPlate> getAllPlateList() {
        List<BBSPlate> plateList = backPlateService.getAllPlateList();
        return plateList;
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxJson AddPlate(HttpServletRequest request){
        AjaxJson json = new AjaxJson();
        Map parameterMap = request.getParameterMap();
        Map<String, Object> dataMap = CommUtils.mapConvert(parameterMap);
        json.setMsg("成功添加论坛板块");
        return json;
    }
    @RequestMapping(value = "/setPlate", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> setPlate(BBSPlate plate) throws Exception {
        logger.info("设置论坛板块[新增或更新]！plate:" + plate);
        Map<String,Object> data = new HashMap();
        if(StringUtils.isBlank(plate.getId())){
            data = backPlateService.addPlate(plate);
        }else{
            data = backPlateService.updatePlate(plate);
        }
        return data;
    }
    @PostMapping("updatePlateStatus")
    @ResponseBody
    public Map<String,Object> updateRoleStatus(@RequestParam("id") String id,@RequestParam("status") Integer status) {
        logger.info("删除/恢复板块！id:" + id+" status:"+status);
        Map<String, Object> data = new HashMap<>();
        if(status == 0){
            //删除板块
            data = backPlateService.delPlate(id,status);
        }else{
            //恢复板块
            data = backPlateService.recoverPlate(id,status);
        }
        return data;
    }
}
